


import 'package:xim/common/get_style_view.dart';
import 'package:xim/http/apis/message/message_api.dart';
import 'package:xim/http/apis/message/message_model.dart';
import 'package:xim/http/ase.dart';
import 'package:xim/http/rsa.dart';
import 'package:xim/http/store.dart';

class MessageDetailController extends GetxStateMixinController<ChatMessageModel>{

  final int messageId;

  /// 消息
  late final ChatMessageModel chatMessage;

  /// 用户私钥
  late final String privateKey;

  /// 明文用户私钥
  late final String plaintextMsgKey;

  /// 明文聊天内容
  late final String plaintextContent;

  MessageDetailController({required this.messageId});

  @override
  Future<ChatMessageModel?> dataInit() async {
    chatMessage = await MessageApi.queryMessageById(messageId);
    privateKey = (await secureTake(StoreKey.privateKey))!;
    plaintextMsgKey = decodeByPrivateKeySync(privateKey, chatMessage.secretKey.secretKey);
    plaintextContent = AES.decryptAes(encryptedBase64Text: chatMessage.content, aseKey: plaintextMsgKey);
    return chatMessage;
  }

}